<?php

	session_start();
	require("connect.php");
	$link = connect_APO();
	require("check.php");
	is_logged_in();
	is_admin();

	
	require("header+logoutB.php");
	headerL();
	

	if($_FILES["uploadedfile"]["type"] != "application/vnd.ms-excel"){
		
	}

	$fileName = $_FILES["uploadedfile"]["tmp_name"];
	$lines = file($fileName);
	
	echo "<br><br>";
	$sql_beginning = "INSERT INTO `users` ( `ID` , `Fname` , `Lname` , `PhoneNumber` , `TotCredit` , `ML` , `Pass` , `Pending` , `MC` , `Status` , `PendingLocation` )
VALUES";
	$sql_end = "";
	$type = "";
	require("parse.php");
	
	$t = true;
	$numberShouldAdd = 0;
	$totAdded = 0;
	
	for($i = 0; $i < sizeof($lines); $i++){
		$parsed = parse($lines[$i]);
		for($j=0; $j<1; $j++){
			if ($parsed[$j][0] == "PLEDGES"){
				$type = "Pledge";
				break;
			} elseif ($parsed[$j][0] == "First Name"){
				break;
			} elseif ($parsed[$j][0] == "BROTHERS"){
				$type = "Brother";
				break;
			} elseif ($parsed[$j][0] == "GRADS/SUPERBROS"){
				$done = true;
				break;
			}
			if($parsed[0][0] == ""){
				break;
			}
			$name = $parsed[$j][0];
			$lastname = $parsed[$j][1];
			$phone = $parsed[$j][5];
			$emailID = $parsed[$j][7];
			
			$sql_end = " ('$emailID', '$name', '$lastname', '$phone', '0', '0', PASSWORD('$emailID!!'), '0', '0', '$type', '');";	
			$sql = $sql_beginning.$sql_end;	 
			//echo $sql;
			$result = mysql_query($sql);
			if(1 != mysql_affected_rows()){
				echo "<div style='padding-left:200;'>".$name." ".$lastname." not added</div></br>";
			} else{
				echo "<div'>".$name." ".$lastname." added</div></br>";
				$totAdded += 1;
			}
			$numberShouldAdd += 1;
		}
		if($done){
			break;
		}
	}

	echo "</br>".$totAdded." out of $numberShouldAdd brothers/pledges were added.";
	
	require("footer.php");
	footer();
	
	mysql_close($link);
?>